package com.waverider.promotion.web;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.servlet.ModelAndView;


public abstract class AbstractLoginedController extends AbstractController
{
	
	@Override
	protected ModelAndView handle(HttpServletRequest request, HttpServletResponse response) throws Exception
	{
		if(!isLogined(request.getSession(true)))
		{
			ModelAndView mv = new ModelAndView("bg/error");
			
			mv.getModel().put(ERROR_MSG, NOT_LOGIN_ERROR_MSG);
			
			return mv;
		}
		
		ModelAndView mv = handleLogined(request, response);
		if(mv != null)
		{
			HttpSession session = request.getSession();
			Map<String, Object> context = mv.getModel();
			// vistor id
			context.put("vistorId", getUserId(session));
			// vistor nick
			context.put("vistorNick", getUserNick(session));
		}
		
		return mv;
	}
	
	protected abstract ModelAndView handleLogined(HttpServletRequest request, HttpServletResponse response) throws Exception;

}
